import QQMap from '../../../utils/qqmap'

// map.js
Page({
  options: {
    pureDataPattern: /^_/, // 指定所有 _ 开头的数据字段为纯数据字段
  },
  data: {
    _id: '',
    // 地图路线
    polyline: [],
    latitude: 28.216207,
    longitude: 112.862157,
    repairInfo: {},

    // 注意：实际开发中，这两个标记点，是从后端接口返回的经纬度，这里仅用于测试开发
    markers: [
      {
        id: 1,
        latitude: 28.216207,
        longitude: 112.862157,
        width: 24,
        height: 30,
      },
      {
        id: 2,
        latitude: 28.216249,
        longitude: 112.860113,
        iconPath: '/static/images/marker.png',
        callout: { content: '飞奔中...', color: '#333', padding: 10, borderRadius: 5, display: 'BYCLICK' },
        width: 40,
        height: 40,
      },
    ],
  },

  goEdit() {
    wx.navigateTo({
      url: '/repair_pkg/pages/form-edit/index?id=' + this.data.repairInfo.id,
    })
  },

  async onShow() {
    const res = await wx.http({
      url: '/repair/' + this.data._id,
    })

    this.setData({
      repairInfo: res.data,
    })

    // this.setData({
    //   ...this.data,
    //   ...res.data
    // })

    if (this.data.repairInfo.status === 1) {
      // if (this.data.repairInfo.status === 2) {

      const [to, from] = this.data.markers.map(({ latitude, longitude }) => ({ latitude, longitude }))

      const res = await QQMap.p.direction({
        mode: 'bicycling',
        from,
        to,
      })

      // 坐标解压
      const coors = res.result.routes[0].polyline,
        points = []
      for (let i = 2; i < coors.length; i++) {
        coors[i] = coors[i - 2] + coors[i] / 1000000
      }

      // 将解压后的坐标放入点串数组pl中
      for (var i = 0; i < coors.length; i += 2) {
        points.push({ latitude: coors[i], longitude: coors[i + 1] })
      }

      this.setData({
        polyline: [{ points, width: 4, color: '#5591af', arrowLine: true }],
      })
    }
  },

  async onLoad({ id }) {
    this.setData({
      _id: id,
    })
  },

  // 这个方法不能在wxml中用{{ getMsg() }}显示返回值
  getMsg() {
    return 'hello heheda~'
  },
})
